### Replication Material ###
### The Manifesto Corpus ### 
### Corpus coverage - Table 1 ###

library(manifestoR)

# set api key
mp_setapikey(key.file = "manifesto_apikey.txt")

# specify corpus version
mp_use_corpus_version("2016-1")


### FUNCTIONS

catz <- function(...) { cat(...,
                            file = outfile,
                            append = TRUE,
                            sep = "") }

formatavl <- function(cdata, vname) {
   ndocs <- sum(unlist(mp_availability(cdata)$availability[, vname]), na.rm = TRUE)
   totaldocs <- nrow(cdata)
   catz(" & ", ndocs)
}

nr.annotations <- function(cdata) {
   nrann <- length(na.omit(codes(mp_corpus(cdata))))
   totalann <<- totalann + nrann
   return(nrann)
}

make.row <- function(cdata, countryname, ann=NULL) {
   if (countryname == "Czech Republic") {
    countryname <- "Czech Rep."   
   }
   if (sum(unlist(mp_availability(cdata)$availability[, "manifestos"]))>0) {
      catz(countryname)
      formatavl(cdata, "manifestos")
      formatavl(cdata, "annotations")
      if(is.null(ann)) {
         catz(" & ", nr.annotations(cdata))
      } else {
         catz(" & ", ann)
      }
   
   catz("\\\\  \n")
   }
   
}


### SCRIPT PART

mpds <- mp_maindataset()
mpds$countryname <- as.character(mpds$countryname)

outfile <- "coverage.tex"
cat(file = outfile, append = FALSE)
totalann <- 0
countryids <- unique(mpds$country)[order(unique(mpds$countryname))]

catz("\\begin{tabular}{p{2cm}llll}\n\\hline\n")
catz("Country  & (1) & (2) & (3) \\\\ \\hline\n")

sapply(countryids, function(countryid) {
   cdata <- subset(mpds, country == countryid)
   make.row(cdata, unique(cdata$countryname))
})

catz("\\hline\n")
make.row(mpds, "Total", ann = totalann)
catz("\\end{tabular}\n\n")
